package com.ev.hr.service.impl;

import com.ev.custom.domain.ContentAssocDO;
import com.ev.custom.service.ContentAssocService;
import com.ev.framework.config.ConstantForDevice;
import com.ev.framework.config.ConstantForHr;
import com.ev.framework.config.ConstantForMES;
import com.ev.hr.vo.ProfessionalSkillsSaveParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ev.hr.dao.ProfessionalSkillsDao;
import com.ev.hr.domain.ProfessionalSkillsDO;
import com.ev.hr.service.ProfessionalSkillsService;
import com.ev.hr.vo.ProfessionalSkillsPageParam;
import com.ev.framework.utils.R;
import com.ev.apis.model.DsResultResponse;
import com.google.common.collect.Maps;


@Service
public class ProfessionalSkillsServiceImpl implements ProfessionalSkillsService {
	@Autowired
	private ContentAssocService contentAssocService;

	@Autowired
	private ProfessionalSkillsDao professionalSkillsDao;
	
	@Override
	public ProfessionalSkillsDO get(Long id){
		return professionalSkillsDao.get(id);
	}
	
	@Override
	public List<ProfessionalSkillsDO> list(Map<String, Object> map){
		return professionalSkillsDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return professionalSkillsDao.count(map);
	}
	
	@Override
	public int save(ProfessionalSkillsDO professionalSkills){
		return professionalSkillsDao.save(professionalSkills);
	}
	
	@Override
	public int update(ProfessionalSkillsDO professionalSkills){
		return professionalSkillsDao.update(professionalSkills);
	}

	@Override
	public int updateAll(ProfessionalSkillsDO professionalSkills){
		return professionalSkillsDao.updateAll(professionalSkills);
	}
	
	@Override
	public int remove(Long id){
		return professionalSkillsDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return professionalSkillsDao.batchRemove(ids);
	}

	@Override
	public R saveAndVerify(ProfessionalSkillsSaveParam saveParam){
		/**
		 * 先保存主表信息
		 */
		ProfessionalSkillsDO professionalSkills = saveParam.getProfessionalSkill();
		if(professionalSkills.getId()!=null){
			professionalSkillsDao.update(professionalSkills);
		}else{
			professionalSkillsDao.save(professionalSkills);
		}
		/**
		 * 先删除附件
		 */
		contentAssocService.removeByAssocIdAndType(new Long[]{professionalSkills.getId()}, ConstantForHr.PROFESS_SKILL_CONTENT);
		/**
		 * 保存附件信息
		 */
		contentAssocService.saveListForEntity(professionalSkills.getId(),saveParam.getContentAssocList(), ConstantForHr.PROFESS_SKILL_CONTENT);
		return R.ok();
	}

	@Override
	public Map<String, Object> getById(Long id) {
		/**
		 * 获取主体信息
		 */
		ProfessionalSkillsDO professionalSkills = professionalSkillsDao.get(id);
		/**
		 * 获取附件数据
		 */
		List<ContentAssocDO> contentAssocList = contentAssocService.list(new HashMap<String,Object>(2){{put("assocId",id);put("assocType",ConstantForHr.PROFESS_SKILL_CONTENT);}});
		/**
		 * 结果组装
		 */
		ProfessionalSkillsSaveParam professionalSkillsDetail = new ProfessionalSkillsSaveParam();
		professionalSkillsDetail.setProfessionalSkill(professionalSkills);
		professionalSkillsDetail.setContentAssocList(contentAssocList);
		if (professionalSkills != null) {
			Map<String,Object> result = Maps.newHashMap();
			result.put("data",professionalSkillsDetail);
			return R.ok(result);
		}
		return R.error();
	}

	@Override
	public Map<String, Object> pageList(ProfessionalSkillsPageParam pageParam) {
		Map<String,Object> result = Maps.newHashMap();
		List<Map<String, Object>> data = professionalSkillsDao.pageList(pageParam);
		int total = professionalSkillsDao.pageCount(pageParam);
		result.put("data", new DsResultResponse(pageParam.getPageno(),pageParam.getPagesize(),total,data));
		return R.ok(result);
	}

}
